/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.CourseInfoClockSource;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.CourseInfoClockSourceRepository;
import me.zhengjie.modules.system.service.CourseInfoClockSourceService;
import me.zhengjie.modules.system.service.dto.CourseInfoClockSourceDto;
import me.zhengjie.modules.system.service.dto.CourseInfoClockSourceQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.CourseInfoClockSourceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wangyu
* @date 2024-03-21
**/
@Service
@RequiredArgsConstructor
public class CourseInfoClockSourceServiceImpl implements CourseInfoClockSourceService {

    private final CourseInfoClockSourceRepository courseInfoClockSourceRepository;
    private final CourseInfoClockSourceMapper courseInfoClockSourceMapper;

    @Override
    public Map<String,Object> queryAll(CourseInfoClockSourceQueryCriteria criteria, Pageable pageable){
        Page<CourseInfoClockSource> page = courseInfoClockSourceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(courseInfoClockSourceMapper::toDto));
    }

    @Override
    public List<CourseInfoClockSourceDto> queryAll(CourseInfoClockSourceQueryCriteria criteria){
        return courseInfoClockSourceMapper.toDto(courseInfoClockSourceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CourseInfoClockSourceDto findById(Long sourceId) {
        CourseInfoClockSource courseInfoClockSource = courseInfoClockSourceRepository.findById(sourceId).orElseGet(CourseInfoClockSource::new);
        ValidationUtil.isNull(courseInfoClockSource.getSourceId(),"CourseInfoClockSource","sourceId",sourceId);
        return courseInfoClockSourceMapper.toDto(courseInfoClockSource);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseInfoClockSourceDto create(CourseInfoClockSource resources) {
        return courseInfoClockSourceMapper.toDto(courseInfoClockSourceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CourseInfoClockSource resources) {
        CourseInfoClockSource courseInfoClockSource = courseInfoClockSourceRepository.findById(resources.getSourceId()).orElseGet(CourseInfoClockSource::new);
        ValidationUtil.isNull( courseInfoClockSource.getSourceId(),"CourseInfoClockSource","id",resources.getSourceId());
        courseInfoClockSource.copy(resources);
        courseInfoClockSourceRepository.save(courseInfoClockSource);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long sourceId : ids) {
            courseInfoClockSourceRepository.deleteById(sourceId);
        }
    }

    @Override
    public void download(List<CourseInfoClockSourceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CourseInfoClockSourceDto courseInfoClockSource : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程编号", courseInfoClockSource.getCourseNo());
            map.put("班级id", courseInfoClockSource.getClassId());
            map.put("班级编号", courseInfoClockSource.getClassNo());
            map.put("班级名称", courseInfoClockSource.getClassName());
            map.put("创建时间", courseInfoClockSource.getCreateTime());
            map.put("创建用户", courseInfoClockSource.getCreateby());
            map.put("更新时间", courseInfoClockSource.getUpdateTime());
            map.put("更新用户", courseInfoClockSource.getUpdateby());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}